<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Docker 笔记.md | 小波的博客</title><meta name="keywords" content="Docker"><meta name="author" content="Steve"><meta name="copyright" content="Steve"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Docker 笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker 笔记.md">
<meta property="og:url" content="https://steve6636.gitee.io/2022/03/06/Docker%20%E7%AC%94%E8%AE%B0_20210404/index.html">
<meta property="og:site_name" content="小波的博客">
<meta property="og:description" content="Docker 笔记">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/forkiller/picture/raw/master/img/20220306223850.jpg">
<meta property="article:published_time" content="2022-03-06T14:29:58.000Z">
<meta property="article:modified_time" content="2022-03-06T14:46:24.517Z">
<meta property="article:author" content="Steve">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/forkiller/picture/raw/master/img/20220306223850.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://steve6636.gitee.io/2022/03/06/Docker%20%E7%AC%94%E8%AE%B0_20210404/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-03-06 22:46:24'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="小波的博客" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210327175316.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 工具箱</span></a></div><div class="menus_item"><a class="site-page" href="/friends"><i class="fa-fw fas fa-address-book"></i><span> Friends</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/forkiller/picture/raw/master/img/20220306223850.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小波的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 工具箱</span></a></div><div class="menus_item"><a class="site-page" href="/friends"><i class="fa-fw fas fa-address-book"></i><span> Friends</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Docker 笔记.md</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-03-06T14:29:58.000Z" title="发表于 2022-03-06 22:29:58">2022-03-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-03-06T14:46:24.517Z" title="更新于 2022-03-06 22:46:24">2022-03-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Docker/">Docker</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>21分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Docker-笔记"><a href="#Docker-笔记" class="headerlink" title="Docker 笔记"></a>Docker 笔记</h1><p>原文地址：<a target="_blank" rel="noopener" href="https://www.yuque.com/nbutluo/env/docker#ZkNfI">https://www.yuque.com/nbutluo/env/docker#ZkNfI</a></p>
<blockquote>
<p>Docker 是基于 Go 语言实现的云开源项目</p>
</blockquote>
<h2 id="Docker-运行原理"><a href="#Docker-运行原理" class="headerlink" title="Docker 运行原理"></a>Docker 运行原理</h2><blockquote>
<p>Docker 是一个 Client-Server 结构的系统，Docker 守护进程运行在主机上， 然后通过 Socket 连接从客户端访问，守护进程从客户端接受命令并管理运行在主机上的容器。 容器，是一个运行时环境，就是我们前面说到的集装箱。<strong>“一次封装，到处运行”</strong> 解决了运行环境和配置问题软件容器，方便做持续集成并有助于整体发布的容器虚拟化技术。</p>
</blockquote>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183022.png"></p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183036.png" alt="image"></p>
<p>容器与虚拟机比较</p>
<p>​       下面的图片比较了 Docker 和传统虚拟化方式的不同之处，可见容器是在操作系统层面上实现虚拟化，直接复用本地主机的操作系统，而传统方式则是在硬件层面实现。</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183047.png" alt="image"></p>
<p>与传统的虚拟机相比，Docker 优势体现为启动速度快、占用体积小。</p>
<h2 id="Docker-的组成"><a href="#Docker-的组成" class="headerlink" title="Docker 的组成"></a>Docker 的组成</h2><blockquote>
<p>Docker 三要素：镜像、容器、仓库</p>
<p><strong>容器</strong>：实例对象，是一个集装箱，<strong>可以把容器看作简易版的 Linux 环境</strong>；<br><strong>镜像</strong>：只读的模板，用来创建容器。可以创建多个容器，例如 mysql redis centOS 等；<br><strong>仓库</strong>：集中存放镜像的。</p>
</blockquote>
<h3 id="镜像（image）"><a href="#镜像（image）" class="headerlink" title="镜像（image）"></a>镜像（image）</h3><blockquote>
<p>镜像是构建 Docker 的基石。用户基于镜像来运行自己的容器。</p>
<p>镜像也是 Docker 生命周期中的 “构建” 部分，镜像是基于联合文件系统的一种层式结构，由一系列指令一步一步构建出来。</p>
</blockquote>
<h5 id="镜像与容器的关系"><a href="#镜像与容器的关系" class="headerlink" title="镜像与容器的关系"></a>镜像与容器的关系</h5><p><strong>镜像是构建 Docker 的基石。用户基于镜像来运行自己的容器。</strong>    Docker 可以帮助你构建和部署容器，你只需要把自己的应用程序或者服务打包放进容器即可。容器是基于镜像启动起来的，容器中可以运行一个或多个进程。我们可以认为，镜像是 Docker 生命周期中的构建或者打包阶段，而容器则是启动或者执行阶段。  容器基于镜像启动，一旦容器启动完成后，我们就可以登录到容器中安装自己需要的软件或者服务。</p>
<h3 id="容器（container）"><a href="#容器（container）" class="headerlink" title="容器（container）"></a>容器（container）</h3><blockquote>
<p><strong>容器是基于镜像启动起来的</strong>，容器中可以运行一个或多个进程。</p>
</blockquote>
<p>容器就是：一个镜像格式；一些列标准操作；一个执行环境。<strong>有镜像才能创建容器，这是根本前提，</strong>和集装箱一样，Docker 在执行上述操作时，并不关心容器中到底装了什么，它不管是 web 服务器，还是数据库，或者是应用程序服务器什么的。所有的容器都按照相同的方式将内容 “装载” 进去。</p>
<p>​     Docker 也不关心你要把容器运到何方：我们可以在自己的笔记本中构建容器，上传到 Registry，然后下载到一个物理的或者虚拟的服务器来测试，在把容器部署到具体的主机中。像标准集装箱一样，Docker 容器方便替换，可以叠加，易于分发，并且尽量通用。</p>
<h3 id="仓库-Registry"><a href="#仓库-Registry" class="headerlink" title="仓库 (Registry)"></a><a target="_blank" rel="noopener" href="https://hub.docker.com/">仓库</a> (Registry)</h3><blockquote>
<p>Registry 用于保存用户构建的镜像，也叫做注册中心。</p>
</blockquote>
<p>Registry 分为公共和私有两种。Docker 公司运营公共的 Registry 叫做 Docker Hub。用户可以在 Docker Hub 注册账号，分享并保存自己的镜像。</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183055.png" alt="image.png"></p>
<h2 id="Docker-安装"><a href="#Docker-安装" class="headerlink" title="Docker 安装"></a>Docker 安装</h2><blockquote>
<p>Docker 官方建议在 Ubuntu 中安装，以下是在 centos 系统的操作</p>
</blockquote>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p>首先确定是 CentOS7 及以上版本，并且可以连接外网</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat &#x2F;etc&#x2F;redhat-release</span><br></pre></td></tr></table></figure>

<p>yum 安装 gcc 相关</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yum -y install gcc</span><br><span class="line">yum -y install gcc-c++</span><br></pre></td></tr></table></figure>

<p>卸载旧版本（首次安装跳过此步骤）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">yum -y remove docker docker-common docker-selinux docker-engine</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; docker官网命令</span><br><span class="line">yum remove docker \</span><br><span class="line">docker-client \</span><br><span class="line">docker-client-latest \</span><br><span class="line">docker-common \</span><br><span class="line">docker-latest \</span><br><span class="line">docker-latest-logrotate \</span><br><span class="line">docker-logrotate \</span><br><span class="line">docker-selinux \</span><br><span class="line">docker-engine-selinux \</span><br><span class="line">docker-engine</span><br></pre></td></tr></table></figure>



<p>安装需要的软件包， yum-util 提供 yum-config-manager 功能，另外两个是 devicemapper 驱动依赖的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo yum install -y yum-utils device-mapper-persistent-data lvm2</span><br></pre></td></tr></table></figure>

<p>设置 stable 镜像仓库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#官方的（大坑）</span><br><span class="line">yum-config-manager --add-repo https:&#x2F;&#x2F;download.docker.com&#x2F;linux&#x2F;centos&#x2F;docker-ce.repo</span><br><span class="line"></span><br><span class="line">推荐使用阿里镜像库</span><br><span class="line">yum-config-manager --add-repo http:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;docker-ce&#x2F;linux&#x2F;centos&#x2F;docker-ce.repo</span><br></pre></td></tr></table></figure>

<p>yum 包更新到最新，更新 yum 软件包索引</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yum makecache fast</span><br><span class="line">&#x2F;&#x2F; 或者使用下面的命令</span><br><span class="line">sudo yum update</span><br></pre></td></tr></table></figure>



<h3 id="安装-docker"><a href="#安装-docker" class="headerlink" title="安装 docker"></a>安装 docker</h3><blockquote>
<p>ce 代表是社区免费版</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install docker-ce</span><br></pre></td></tr></table></figure>

<p>manjaro-xfce 安装 docker</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 更新系统</span><br><span class="line">sudo pacman -Syyu</span><br><span class="line"></span><br><span class="line"># 安装 docker</span><br><span class="line">sudo pacman -Sy docker</span><br><span class="line"></span><br><span class="line"># 启动 docker</span><br><span class="line">sudo systemctl start docker</span><br><span class="line"></span><br><span class="line"># 查看运行状态</span><br><span class="line">sudo systemctl status docker</span><br></pre></td></tr></table></figure>

<h3 id="启动-docker"><a href="#启动-docker" class="headerlink" title="启动 docker"></a>启动 docker</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start docker</span><br></pre></td></tr></table></figure>

<h3 id="测试-run"><a href="#测试-run" class="headerlink" title="测试 (run)"></a>测试 (run)</h3><p>安装后查看 docker 版本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker -v</span><br><span class="line">docker version</span><br><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure>

<h3 id="配置镜像加速"><a href="#配置镜像加速" class="headerlink" title="配置镜像加速"></a>配置镜像加速</h3><blockquote>
<p>登录自己的阿里云账户，配置<a target="_blank" rel="noopener" href="https://cr.console.aliyun.com/cn-hangzhou/instances/repositories">镜像加速器服务</a></p>
</blockquote>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183112.png" alt="image.png"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sudo mkdir -p &#x2F;etc&#x2F;docker</span><br><span class="line">sudo tee &#x2F;etc&#x2F;docker&#x2F;daemon.json &lt;&lt;-&#39;EOF&#39;</span><br><span class="line">&#123;</span><br><span class="line">  &quot;registry-mirrors&quot;: [&quot;https:&#x2F;&#x2F;haqud1a3.mirror.aliyuncs.com&quot;]</span><br><span class="line">&#125;</span><br><span class="line">EOF</span><br><span class="line">sudo systemctl daemon-reload</span><br><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure>

<p>或者使用中国科技大学的镜像源</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">vi &#x2F;etc&#x2F;docker&#x2F;daemon.json</span><br><span class="line">&#x2F;&#x2F; 添加以下代码</span><br><span class="line">&#123;</span><br><span class="line">&quot;registry-mirrors&quot;: [&quot;https:&#x2F;&#x2F;docker.mirrors.ustc.edu.cn&quot;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编辑并保存完成后，重新启动 docker 服务，配置生效</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service restart docker</span><br></pre></td></tr></table></figure>

<p><strong>检测加速是否生效</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef|grep docker</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183118.png" alt="图像.png"></p>
<h3 id="其他系统命令"><a href="#其他系统命令" class="headerlink" title="其他系统命令"></a>其他系统命令</h3><table>
<thead>
<tr>
<th>停止 docker</th>
<th>systemctl stop docker</th>
</tr>
</thead>
<tbody><tr>
<td>重启 docker</td>
<td>systemctl restart docker</td>
</tr>
<tr>
<td>开机启动</td>
<td>systemctl enable docker</td>
</tr>
<tr>
<td>查看 docker 状态</td>
<td>systemctl status docker</td>
</tr>
<tr>
<td>开机启动</td>
<td>systemctl enable docker</td>
</tr>
<tr>
<td>查看 docker 概要信息</td>
<td>docker info</td>
</tr>
<tr>
<td>查看 docker 帮助文档</td>
<td>docker –help</td>
</tr>
</tbody></table>
<h3 id="卸载-docker"><a href="#卸载-docker" class="headerlink" title="卸载 docker"></a>卸载 docker</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop docker </span><br><span class="line">yum -y remove docker-ce</span><br><span class="line">rm -rf &#x2F;var&#x2F;lib&#x2F;docker</span><br></pre></td></tr></table></figure>

<h2 id="Docker-常用命令"><a href="#Docker-常用命令" class="headerlink" title="Docker 常用命令"></a>Docker 常用命令</h2><h3 id="帮助命令"><a href="#帮助命令" class="headerlink" title="帮助命令"></a>帮助命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker version</span><br><span class="line">docker -v</span><br><span class="line">docker info</span><br><span class="line">docker --help</span><br></pre></td></tr></table></figure>

<h3 id="镜像命令"><a href="#镜像命令" class="headerlink" title="镜像命令"></a>镜像命令</h3><h4 id="查看镜像"><a href="#查看镜像" class="headerlink" title="查看镜像"></a>查看镜像</h4><blockquote>
<p>列出本地主机上的镜像</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183123.png" alt="图像.png"></p>
<p>各个选项说明:</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183126.svg" alt="img"></p>
<p>同一仓库源可以有多个 TAG，代表这个仓库源的不同个版本，我们使用 REPOSITORY:TAG 来定义不同的镜像。</p>
<p>如果你不指定一个镜像的版本标签，例如你只使用 ubuntu，docker 将默认使用 ubuntu:latest 镜像</p>
<h4 id="搜索镜像"><a href="#搜索镜像" class="headerlink" title="搜索镜像"></a>搜索镜像</h4><blockquote>
<p>查找需要的镜像，可以通过以下命令搜索，查找网站 <a target="_blank" rel="noopener" href="https://hub.docker.com/">https://hub.docker.com</a></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search 镜像名称</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183130.svg" alt="img"></p>
<blockquote>
<p>ps：docker search -s 30 语法已经不再使用！ </p>
<p>新版本的 Docker 请使用 docker search -f stars=30 imageName 来搜索镜像</p>
<p>示例：docker search -f stars=10 java</p>
</blockquote>
<h4 id="拉取镜像"><a href="#拉取镜像" class="headerlink" title="拉取镜像"></a>拉取镜像</h4><blockquote>
<p>用于下载镜像，可以从 <a target="_blank" rel="noopener" href="https://hub.docker.com/">https://hub.docker.com</a> 中查找下载</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull 镜像名称</span><br></pre></td></tr></table></figure>

<h4 id="删除镜像"><a href="#删除镜像" class="headerlink" title="删除镜像"></a>删除镜像</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker rmi 镜像名称或镜像ID</span><br><span class="line">i&#x3D;image</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183135.svg" alt="img"></p>
<h3 id="容器命令"><a href="#容器命令" class="headerlink" title="容器命令"></a>容器命令</h3><h4 id="查看容器"><a href="#查看容器" class="headerlink" title="查看容器"></a>查看容器</h4><table>
<thead>
<tr>
<th>docker ps</th>
<th>查看正在运行的容器</th>
</tr>
</thead>
<tbody><tr>
<td>docker ps -a</td>
<td>查看所有容器 (正在运行的 + 运行过的)</td>
</tr>
<tr>
<td>docker ps -l</td>
<td>查看最后一次运行的容器</td>
</tr>
<tr>
<td>docker ps -q</td>
<td>静默模式，只显示容器编号</td>
</tr>
<tr>
<td>docker ps -n 3</td>
<td>显示最近 n 个创建的容器</td>
</tr>
<tr>
<td><code>docker ps -f status=exited</code></td>
<td>查看停止的容器</td>
</tr>
<tr>
<td>docker ps –no-trunc</td>
<td>不截断输出。</td>
</tr>
</tbody></table>
<h4 id="新建并启动容器"><a href="#新建并启动容器" class="headerlink" title="新建并启动容器"></a>新建并启动容器</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run [OPTIONS] IMAGE [COMMAND] [ARG...]</span><br></pre></td></tr></table></figure>

<p>参数说明：</p>
<table>
<thead>
<tr>
<th>–name</th>
<th>为容器指定一个名称</th>
</tr>
</thead>
<tbody><tr>
<td>-d</td>
<td>后台运行容器，并返回容器 ID，也即启动守护式容器</td>
</tr>
<tr>
<td>-i</td>
<td>以交互模式运行容器，通常与 -t 同时使用；</td>
</tr>
<tr>
<td>-t</td>
<td>为容器重新分配一个伪输入终端，通常与 -i 同时使</td>
</tr>
<tr>
<td>-P(大写)</td>
<td>随机端口映射</td>
</tr>
<tr>
<td>-p(小写)</td>
<td>指定端口映射</td>
</tr>
</tbody></table>
<h4 id="容器启动停止重启"><a href="#容器启动停止重启" class="headerlink" title="容器启动停止重启"></a>容器启动停止重启</h4><table>
<thead>
<tr>
<th>启动容器</th>
<th>docker start 容器 ID 或者容器名</th>
</tr>
</thead>
<tbody><tr>
<td>重启容器</td>
<td>docker restart 容器 ID 或者容器名</td>
</tr>
<tr>
<td>停止容器</td>
<td>docker stop 容器 ID 或者容器名</td>
</tr>
<tr>
<td>强制停止容器</td>
<td>docker kill 容器 ID 或者容器名</td>
</tr>
</tbody></table>
<h4 id="退出容器"><a href="#退出容器" class="headerlink" title="退出容器"></a>退出容器</h4><blockquote>
<p>有两种方式</p>
</blockquote>
<table>
<thead>
<tr>
<th>exit</th>
<th>容器停止退出</th>
</tr>
</thead>
<tbody><tr>
<td>ctrl+P+Q</td>
<td>容器不停止退出</td>
</tr>
</tbody></table>
<h4 id="删除容器"><a href="#删除容器" class="headerlink" title="删除容器"></a>删除容器</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker rm 容器ID</span><br></pre></td></tr></table></figure>

<p><strong>一次性删除多个容器，有以下三种方式</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker rm -f $(docker ps -a -q)</span><br><span class="line"></span><br><span class="line">docker ps -a -q | xargs docker rm</span><br><span class="line"></span><br><span class="line">docker rm -f &#96;docker ps -q&#96;</span><br></pre></td></tr></table></figure>

<h4 id="启动交互式容器"><a href="#启动交互式容器" class="headerlink" title="启动交互式容器"></a>启动交互式容器</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it centos &#x2F;bin&#x2F;bash </span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183141.png" alt="image.png"></p>
<h4 id="启动守护式容器"><a href="#启动守护式容器" class="headerlink" title="启动守护式容器"></a>启动守护式容器</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d 容器名</span><br></pre></td></tr></table></figure>

<p>使用镜像 centos:latest 以后台模式启动一个容器，docker run -d centos，然后 docker ps -a 进行查看，会发现容器已经退出，<strong>Docker 容器后台运行，就必须有一个前台进程。</strong></p>
<p>docker 的机制是<strong>：容器运行的命令如果不是那些一直挂起的命令（比如运行 top，tail），就是会自动退出的。</strong></p>
<p>比如你的 web 容器，我们以 nginx 为例，正常情况下，我们配置启动服务只需要启动响应的 service 即可。例如</p>
<p>service nginx start，但是，这样做，nginx 为后台进程模式运行，就导致 docker 前台没有运行的应用，这样的容器后台启动后，会立即自杀因为他觉得他没事可做了。所以，最佳的解决方案是，将你要运行的程序以前台进程的形式运行。</p>
<h4 id="查看容器日志"><a href="#查看容器日志" class="headerlink" title="查看容器日志"></a>查看容器日志</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker logs -f -t --tail 容器ID</span><br></pre></td></tr></table></figure>

<ul>
<li>-t 是加入时间戳</li>
<li> -f 跟随最新的日志打印</li>
<li> –tail 数字 显示最后多少条</li>
</ul>
<blockquote>
<p>一下示例每隔 2s 输出信息，以守护式方式运行，但是包含前台进程，所以不会终止</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d centos &#x2F;bin&#x2F;sh -c &quot;while true;do echo hello zzyy;sleep 2;done&quot;</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183145.png" alt="image.png"></p>
<h4 id="查看容器内运行的进程"><a href="#查看容器内运行的进程" class="headerlink" title="查看容器内运行的进程"></a>查看容器内运行的进程</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker top 容器ID</span><br></pre></td></tr></table></figure>

<h4 id="查看容器内部细节"><a href="#查看容器内部细节" class="headerlink" title="查看容器内部细节"></a>查看容器内部细节</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect 容器ID</span><br></pre></td></tr></table></figure>

<h4 id="容器间的交互（重点）"><a href="#容器间的交互（重点）" class="headerlink" title="容器间的交互（重点）"></a>容器间的交互（重点）</h4><blockquote>
<p>指的是进入正在运行的容器并以命令行交互</p>
</blockquote>
<h5 id="直接进入容器"><a href="#直接进入容器" class="headerlink" title="直接进入容器"></a>直接进入容器</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker exec -it 容器ID bashShell</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183148.png" alt="image.png"></p>
<h5 id="重新进入退出的容器"><a href="#重新进入退出的容器" class="headerlink" title="重新进入退出的容器"></a>重新进入退出的容器</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker attach 容器ID</span><br></pre></td></tr></table></figure>

<h5 id="上述两个区别"><a href="#上述两个区别" class="headerlink" title="上述两个区别"></a>上述两个区别</h5><ul>
<li>attach 直接进入容器启动命令的终端，不会启动新的进程</li>
<li> exec 是在容器中打开新的终端，并且可以启动新的进程</li>
</ul>
<h5 id="从容器内拷贝文件到主机上"><a href="#从容器内拷贝文件到主机上" class="headerlink" title="从容器内拷贝文件到主机上"></a>从容器内拷贝文件到主机上</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker cp  容器ID:容器内路径 目的主机路径</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183151.png" alt="image.png"></p>
<h2 id="Docker-镜像"><a href="#Docker-镜像" class="headerlink" title="Docker 镜像"></a>Docker 镜像</h2><blockquote>
<p>镜像是基于联合文件 (UnionFS) 系统的一种层式结构，由一系列指令一步一步构建出来，  </p>
<p>（UnionFS）文件系统是一种分层、轻量级并且高性能的文件系统，它支持对文件系统的修改作为一次提交来一层层的叠加。</p>
</blockquote>
<p>特性：一次同时加载多个文件系统，但从外面看起来，只能看到一个文件系统，联合加载会把各层文件系统叠加起来，这样最终的文件系统会包含所有底层的文件和目录。</p>
<h3 id="加载原理"><a href="#加载原理" class="headerlink" title="加载原理"></a>加载原理</h3><blockquote>
<p> docker 的镜像实际上由一层一层的文件系统组成，这种层级的文件系统 UnionFS.</p>
</blockquote>
<p>bootfs (boot file system) 主要包含 <code>bootloader</code> 和 <code>kernel</code>, bootloader 主要是引导加载 kernel, Linux 刚启动时会加载 bootfs 文件系统，在 Docker 镜像的最底层是 bootfs。这一层与我们典型的 Linux/Unix 系统是一样的，包含 boot 加载器和内核。当 boot 加载完成之后整个内核就都在内存中了，此时内存的使用权已由 bootfs 转交给内核，此时系统也会卸载 bootfs.</p>
<p> rootfs (root file system) ，在 bootfs 之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。rootfs 就是各种不同的操作系统发行版，比如 Ubuntu，Centos 等等。 </p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183155.png" alt="image.png"></p>
<h3 id="分层的镜像"><a href="#分层的镜像" class="headerlink" title="分层的镜像"></a>分层的镜像</h3><blockquote>
<p>以我们的 pull 为例，在下载的过程中我们可以看到 docker 的镜像好像是在一层一层的在下载</p>
</blockquote>
<p><strong>最大的一个好处就是 - 共享资源</strong></p>
<p> 比如：有多个镜像都从相同的 base 镜像构建而来，那么宿主机只需在磁盘上保存一份 base 镜像，</p>
<p>同时内存中也只需加载一份 base 镜像，就可以为所有容器服务了。而且镜像的每一层都可以被共享。</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183200.png" alt="image.png"></p>
<h3 id="镜像的特点"><a href="#镜像的特点" class="headerlink" title="镜像的特点"></a>镜像的特点</h3><p>Docker 镜像都是只读的</p>
<p>当容器启动时，一个新的可写层被加载到镜像的顶部。</p>
<p>这一层通常被称作“容器层”，“容器层”之下的都叫“镜像层”。</p>
<h3 id="Docker-镜像-commit"><a href="#Docker-镜像-commit" class="headerlink" title="Docker 镜像 commit"></a>Docker 镜像 commit</h3><blockquote>
<p>docker commit 提交容器副本使之成为一个新的镜像</p>
</blockquote>
<h4 id="基本格式"><a href="#基本格式" class="headerlink" title="基本格式"></a>基本格式</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker commit -m&#x3D;“提交的描述信息” -a&#x3D;“作者” 容器ID 要创建的目标镜像名:[标签名]</span><br><span class="line">m&#x3D;message,a&#x3D;author</span><br></pre></td></tr></table></figure>

<ol>
<li>从 Hub 上下载 tomcat 镜像到本地并成功运行，<code>-P</code> 表示指定端口运行</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -P tomcat</span><br><span class="line">&#x2F;&#x2F; 也可以使用守护式运行，就不会出现编译的界面了</span><br><span class="line">docker run -d -P tomcat</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183204.png" alt="image.png"></p>
<p>查看刚才运行的容器进程</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183207.png" alt="image.png"></p>
<p>浏览器访问是 404</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183210.png" alt="image.png"></p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183217.png" alt="image.png"></p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183225.png" alt="image.png"></p>
<h4 id="生成镜像"><a href="#生成镜像" class="headerlink" title="生成镜像"></a>生成镜像</h4><blockquote>
<p>解决上述问题后，可以将此容器制作为自己的镜像，下次直接运行，避免再出现上述错误</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit -m&#x3D;&#39;my tomcat&#39; -a&#x3D;&#39;your&#39; 30fda7b77bb8 nbutluo&#x2F;tomcat:0.1</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183228.png" alt="image.png"></p>
<p>直接运行刚才自己创建的镜像生成容器</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183231.png" alt="image.png"></p>
<p>在浏览器中运行直接一步到位，不会出错</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183233.png" alt="image.png"></p>
<h2 id="Docker-容器数据卷"><a href="#Docker-容器数据卷" class="headerlink" title="Docker 容器数据卷"></a>Docker 容器数据卷</h2><blockquote>
<p>Docker 容器产生的数据，若不通过 docker commit 生成新的镜像，使数据做为镜像的一部分保存下来，</p>
<p>则当容器删除后，数据自然也就没有了，为了能保存数据在 docker 中我们使用卷。</p>
</blockquote>
<blockquote>
<p>卷就是目录或文件，存在于一个或多个容器中，由 docker 挂载到容器，但不属于联合文件系统，因此能够绕过 Union File System 提供一些用于持续存储或共享数据的特性：</p>
</blockquote>
<p><strong>卷的设计目的是数据持久化，完全独立于容器的生存周期，因此 Docker 不会在容器删除时删除其挂载的数据卷</strong></p>
<p>特点：</p>
<ol>
<li>数据卷可在容器之间共享或重用数据</li>
<li>卷中的更改可以直接生效</li>
<li>数据卷中的更改不会包含在镜像的更新中</li>
<li>数据卷的生命周期一直持续到没有容器使用它为止     </li>
</ol>
<p>​    </p>
<h3 id="数据卷"><a href="#数据卷" class="headerlink" title="数据卷"></a>数据卷</h3><blockquote>
<p>在容器内添加数据卷，有两种方式：命令行添加与 DockerFile 添加</p>
</blockquote>
<h4 id="直接命令行添加"><a href="#直接命令行添加" class="headerlink" title="直接命令行添加"></a>直接命令行添加</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -v &#x2F;宿主机绝对路径目录:&#x2F;容器内目录  镜像名</span><br></pre></td></tr></table></figure>

<h5 id="查看数据卷是否挂载成功"><a href="#查看数据卷是否挂载成功" class="headerlink" title="查看数据卷是否挂载成功"></a>查看数据卷是否挂载成功</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect 容器ID</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183238.png" alt="image.png"></p>
<h5 id="容器和宿主机之间数据共享"><a href="#容器和宿主机之间数据共享" class="headerlink" title="容器和宿主机之间数据共享"></a>容器和宿主机之间数据共享</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183241.png" alt="image.png"></p>
<h5 id="容器停止退出后，主机修改后数据是否同步"><a href="#容器停止退出后，主机修改后数据是否同步" class="headerlink" title="容器停止退出后，主机修改后数据是否同步"></a>容器停止退出后，主机修改后数据是否同步</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183243.png" alt="image.png"></p>
<h4 id="带权限命令添加"><a href="#带权限命令添加" class="headerlink" title="带权限命令添加"></a>带权限命令添加</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -v &#x2F;宿主机绝对路径目录:&#x2F;容器内目录:ro 镜像名</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183249.png" alt="image.png"></p>
<h4 id="DockFile-方式添加"><a href="#DockFile-方式添加" class="headerlink" title="DockFile 方式添加"></a>DockFile 方式添加</h4><h5 id="1-根目录下新建-mydocker-文件夹并进入，编辑一个-DockerFile"><a href="#1-根目录下新建-mydocker-文件夹并进入，编辑一个-DockerFile" class="headerlink" title="1. 根目录下新建 mydocker 文件夹并进入，编辑一个 DockerFile"></a>1. 根目录下新建 mydocker 文件夹并进入，编辑一个 DockerFile</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir mydocker</span><br><span class="line">vim &#x2F;mydocker&#x2F;DockerFile</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可在 Dockerfile 中使用 <strong>VOLUME 指令</strong>来给镜像添加一个或多个数据卷</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">VOLUME[&quot;&#x2F;dataVolumeContainer&quot;,&quot;&#x2F;dataVolumeContainer2&quot;,&quot;&#x2F;dataVolumeContainer3&quot;]</span><br></pre></td></tr></table></figure>

<p> 说明：出于可移植和分享的考虑，用 <code>-v 主机目录：容器目录</code> 这种方法不能够直接在 Dockerfile 中实现。</p>
<p>由于宿主机目录是依赖于特定宿主机的，并不能够保证在所有的宿主机上都存在这样的特定目录。</p>
<h5 id="2-File-构建"><a href="#2-File-构建" class="headerlink" title="2. File 构建"></a>2. File 构建</h5><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## volume test</span></span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">VOLUME</span><span class="bash"> [<span class="string">&quot;/dataVolumeContainer1&quot;</span>,<span class="string">&quot;/dataVolumeContainer2&quot;</span>]<span class="keyword">do</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> <span class="built_in">echo</span> <span class="string">&quot;finished,--------success1&quot;</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> /bin/bash volume <span class="built_in">test</span></span></span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">VOLUME</span><span class="bash"> [<span class="string">&quot;/dataVolumeContainer1&quot;</span>,<span class="string">&quot;/dataVolumeContainer2&quot;</span>]</span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> <span class="built_in">echo</span> <span class="string">&quot;finished,--------success1&quot;</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> /bin/bash</span></span><br></pre></td></tr></table></figure>

<h5 id="3-获得一个新镜像-zzyy-centos"><a href="#3-获得一个新镜像-zzyy-centos" class="headerlink" title="3. 获得一个新镜像 zzyy/centos"></a>3. 获得一个新镜像 zzyy/centos</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -f &#x2F;mydocker&#x2F;DockerFile -t nbutluo&#x2F;centos .</span><br></pre></td></tr></table></figure>

<p>注意末尾的 “.”</p>
<ul>
<li>-f 表示 file</li>
<li>-t 表示命名空间</li>
<li>. 表示当前目录</li>
</ul>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183258.png" alt="image.png"></p>
<h5 id="4-run-运行上一步生成的容器"><a href="#4-run-运行上一步生成的容器" class="headerlink" title="4. run 运行上一步生成的容器"></a>4. run 运行上一步生成的容器</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183303.png" alt="image.png"></p>
<h5 id="5-对应的主机目录地址哪？？"><a href="#5-对应的主机目录地址哪？？" class="headerlink" title="5. 对应的主机目录地址哪？？"></a>5. 对应的主机目录地址哪？？</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183308.png" alt="image.png"></p>
<p><strong>主机对应默认地址</strong></p>
<p>首次</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183310.png" alt="image.png"></p>
<p>然后在主机中根据目录查看，会发现在容器中生成的文件，主机对应的目录也会自动生成</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183314.png" alt="image.png"></p>
<h3 id="数据卷容器"><a href="#数据卷容器" class="headerlink" title="数据卷容器"></a>数据卷容器</h3><blockquote>
<p>命名的容器挂载数据卷，其它容器通过挂载这个 (父容器) 实现数据共享，挂载数据卷的容器，称为数据卷容器</p>
</blockquote>
<h4 id="总体介绍"><a href="#总体介绍" class="headerlink" title="总体介绍"></a>总体介绍</h4><blockquote>
<p><strong>容器之间配置信息的传递，数据卷的生命周期一直持续到没有容器使用它为止</strong></p>
</blockquote>
<p>以上一步新建的镜像 nbutluo/centos 为模板并运行容器 dc01/dc02/dc03</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183317.svg" alt="img"></p>
<h4 id="容器间传递共享"><a href="#容器间传递共享" class="headerlink" title="容器间传递共享"></a>容器间传递共享</h4><blockquote>
<p>容器间传递共享是通过参数 <code>--volumes-from</code> 来实现</p>
</blockquote>
<h5 id="1-先启动一个父容器-dc01"><a href="#1-先启动一个父容器-dc01" class="headerlink" title="1. 先启动一个父容器 dc01"></a>1. 先启动一个父容器 dc01</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183320.png" alt="image.png"></p>
<h5 id="2-dc02-dc03-继承自-dc01"><a href="#2-dc02-dc03-继承自-dc01" class="headerlink" title="2. dc02/dc03 继承自 dc01"></a>2. dc02/dc03 继承自 dc01</h5><blockquote>
<p>使用 <code>--volumes-from</code> </p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --name dc02 --volumes-from dc01 zzyy&#x2F;centos</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183323.png" alt="image.png"></p>
<h5 id="3-回到-dc01-可以看到-02-03-各自添加的都能共享了"><a href="#3-回到-dc01-可以看到-02-03-各自添加的都能共享了" class="headerlink" title="3. 回到 dc01 可以看到 02/03 各自添加的都能共享了"></a>3. 回到 dc01 可以看到 02/03 各自添加的都能共享了</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183326.png" alt="image.png"></p>
<h5 id="删除-dc01，dc02-修改后-dc03-可否访问"><a href="#删除-dc01，dc02-修改后-dc03-可否访问" class="headerlink" title="删除 dc01，dc02 修改后 dc03 可否访问"></a>删除 dc01，dc02 修改后 dc03 可否访问</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183329.png" alt="image.png"></p>
<h5 id="删除-dc02-后-dc03-可否访问，再进一步继承"><a href="#删除-dc02-后-dc03-可否访问，再进一步继承" class="headerlink" title="删除 dc02 后 dc03 可否访问，再进一步继承"></a>删除 dc02 后 dc03 可否访问，再进一步继承</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183333.png" alt="image.png"></p>
<h5 id="新建-dc04-继承-dc03-后再删除-dc03"><a href="#新建-dc04-继承-dc03-后再删除-dc03" class="headerlink" title="新建 dc04 继承 dc03 后再删除 dc03"></a>新建 dc04 继承 dc03 后再删除 dc03</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183335.png" alt="image.png"></p>
<p><strong>结论：容器之间配置信息的传递，数据卷的生命周期一直持续到没有容器使用它为止</strong></p>
<h2 id="DockerFile-解析"><a href="#DockerFile-解析" class="headerlink" title="DockerFile 解析"></a>DockerFile 解析</h2><blockquote>
<p>Dockerfile 是用来构建 Docker 镜像的构建文件，是由一系列命令和参数构成的脚本</p>
</blockquote>
<h3 id="构建三步骤"><a href="#构建三步骤" class="headerlink" title="构建三步骤"></a>构建三步骤</h3><ol>
<li> 编写 Dockerfile 文件</li>
<li> docker build</li>
<li> docker run</li>
</ol>
<h3 id="构建过程解析"><a href="#构建过程解析" class="headerlink" title="构建过程解析"></a>构建过程解析</h3><h5 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h5><ol>
<li>每条保留字指令都必须为大写字母且后面至少要跟随一个参数</li>
<li>指令按照从上到下，顺序执行</li>
<li><code>#</code>表示注释</li>
<li>每条指令都会创建一个新的镜像层，并对镜像进行提交</li>
</ol>
<h5 id="大致流程"><a href="#大致流程" class="headerlink" title="大致流程"></a>大致流程</h5><ol>
<li>docker 从基础镜像运行一个容器</li>
<li>执行一条指令并对容器作出修改</li>
<li>执行类似 docker commit 的操作后提交一个新的镜像层</li>
<li> docker 再基于刚提交的镜像运行一个新容器</li>
<li>执行 dockerfile 中的下一条指令直到所有指令都执行完成</li>
</ol>
<h5 id="小总结"><a href="#小总结" class="headerlink" title="小总结"></a>小总结</h5><p>从应用软件的角度来看，Dockerfile、Docker 镜像与 Docker 容器分别代表软件的三个不同阶段，</p>
<ul>
<li>Dockerfile 是软件的原材料</li>
<li> Docker 镜像是软件的交付品</li>
<li> Docker 容器则可以认为是软件的运行态。</li>
</ul>
<blockquote>
<p>Dockerfile 面向开发，Docker 镜像成为交付标准，Docker 容器则涉及部署与运维，三者缺一不可，合力充当 Docker 体系的基石。</p>
</blockquote>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183340.png" alt="image.png"></p>
<ol>
<li>Dockerfile，需要定义一个 Dockerfile，Dockerfile 定义了进程需要的一切东西。Dockerfile 涉及的内容包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程 (当应用进程需要和系统服务和内核进程打交道，这时需要考虑如何设计 namespace 的权限控制) 等等；</li>
<li>Docker 镜像，在用 Dockerfile 定义一个文件之后，docker build 时会产生一个 Docker 镜像，当运行 Docker 镜像时，会真正开始提供服务；</li>
<li>Docker 容器，容器是直接提供服务的。</li>
</ol>
<h3 id="保留字指令"><a href="#保留字指令" class="headerlink" title="保留字指令"></a>保留字指令</h3><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183344.png" alt="image.png"></p>
<h5 id="FROM"><a href="#FROM" class="headerlink" title="FROM"></a>FROM</h5><blockquote>
<p>基础镜像，当前新镜像是基于哪个镜像的</p>
</blockquote>
<p><strong>MAINTAINER</strong></p>
<blockquote>
<p>镜像维护者的姓名和邮箱地址</p>
</blockquote>
<h5 id="RUN"><a href="#RUN" class="headerlink" title="RUN"></a>RUN</h5><blockquote>
<p>容器构建时需要运行的命令</p>
</blockquote>
<h5 id="EXPOSE"><a href="#EXPOSE" class="headerlink" title="EXPOSE"></a>EXPOSE</h5><blockquote>
<p>当前容器对外暴露出的端口</p>
</blockquote>
<h5 id="WORKDIR"><a href="#WORKDIR" class="headerlink" title="WORKDIR"></a>WORKDIR</h5><blockquote>
<p>指定在创建容器后，终端默认登陆的进来工作目录，一个落脚点</p>
</blockquote>
<h5 id="ENV"><a href="#ENV" class="headerlink" title="ENV"></a>ENV</h5><blockquote>
<p>用来在构建镜像过程中设置环境变量</p>
</blockquote>
<h5 id="ADD"><a href="#ADD" class="headerlink" title="ADD"></a>ADD</h5><blockquote>
<p>将宿主机目录下的文件拷贝进镜像且 ADD 命令会自动处理 URL 和解压 tar 压缩包</p>
</blockquote>
<h5 id="COPY"><a href="#COPY" class="headerlink" title="COPY"></a>COPY</h5><blockquote>
<p>类似 ADD，拷贝文件和目录到镜像中。 将从构建上下文目录中 &lt;源路径&gt; 的文件 / 目录复制到新的一层的镜像内的 &lt; 目标路径 &gt; 位置</p>
</blockquote>
<h5 id="VOLUME"><a href="#VOLUME" class="headerlink" title="VOLUME"></a>VOLUME</h5><blockquote>
<p>容器数据卷，用于数据保存和持久化工作</p>
</blockquote>
<h5 id="CMD"><a href="#CMD" class="headerlink" title="CMD"></a>CMD</h5><blockquote>
<p>指定一个容器启动时要运行的命令，Dockerfile 中可以有多个 CMD 指令，但只有最后一个生效，</p>
<p>CMD 会被 docker run 之后的参数替换</p>
</blockquote>
<h5 id="ENTRYPOINT"><a href="#ENTRYPOINT" class="headerlink" title="ENTRYPOINT"></a>ENTRYPOINT</h5><blockquote>
<p>指定一个容器启动时要运行的命令</p>
<p>ENTRYPOINT 的目的和 CMD 一样，都是在指定容器启动程序及参数</p>
</blockquote>
<h5 id="ONBUILD"><a href="#ONBUILD" class="headerlink" title="ONBUILD"></a>ONBUILD</h5><blockquote>
<p>当构建一个被继承的 Dockerfile 时运行命令，父镜像在被子继承后父镜像的 onbuild 被触发</p>
</blockquote>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183349.png" alt="image.png"></p>
<p>子类继承父类后触发</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183355.png" alt="image.png"></p>
<h3 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h3><h4 id="Base-镜像-scratch"><a href="#Base-镜像-scratch" class="headerlink" title="Base 镜像 (scratch)"></a>Base 镜像 (scratch)</h4><blockquote>
<p>Docker Hub 中 99% 的镜像都是通过在 base 镜像中安装和配置需要的软件构建出来的</p>
</blockquote>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183400.png" alt="image.png"></p>
<h4 id="自定义-centos-镜像"><a href="#自定义-centos-镜像" class="headerlink" title="自定义 centos 镜像"></a>自定义 centos 镜像</h4><h5 id="1-准备-DockerFile"><a href="#1-准备-DockerFile" class="headerlink" title="1. 准备 DockerFile"></a>1. 准备 DockerFile</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim &#x2F;mydocker&#x2F;DockerFile</span><br></pre></td></tr></table></figure>

<p>myCentOS 内容 DockerFile</p>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">MAINTAINER</span> zzyy&lt;zzyy167@<span class="number">126</span>.com&gt;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">ENV</span> MYPATH /usr/local</span><br><span class="line"><span class="keyword">WORKDIR</span><span class="bash"> <span class="variable">$MYPATH</span></span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> yum -y install vim</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> yum -y install net-tools</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">80</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> <span class="built_in">echo</span> <span class="variable">$MYPATH</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> <span class="built_in">echo</span> <span class="string">&quot;success--------------ok&quot;</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> /bin/bash</span></span><br></pre></td></tr></table></figure>

<h5 id="2-构建运行"><a href="#2-构建运行" class="headerlink" title="2. 构建运行"></a>2. 构建运行</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -f &#x2F;mydocker&#x2F;DocckerFile -t 新镜像名字:TAG</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183407.png" alt="image.png"></p>
<h5 id="3-运行"><a href="#3-运行" class="headerlink" title="3. 运行"></a>3. 运行</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it 新镜像名字:TAG </span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183410.png" alt="image.png"></p>
<h5 id="4-列出镜像的变更历史"><a href="#4-列出镜像的变更历史" class="headerlink" title="4. 列出镜像的变更历史"></a>4. 列出镜像的变更历史</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker history 镜像名</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183414.png" alt="image.png"></p>
<h4 id="CMD-ENTRYPOINT-指令对比"><a href="#CMD-ENTRYPOINT-指令对比" class="headerlink" title="CMD/ENTRYPOINT 指令对比"></a>CMD/ENTRYPOINT 指令对比</h4><blockquote>
<p>两个命令的相同点：都是指定一个容器启动时要运行的命令</p>
</blockquote>
<h5 id="CMD-1"><a href="#CMD-1" class="headerlink" title="CMD"></a>CMD</h5><blockquote>
<p><strong>Dockerfile 中可以有多个 CMD 指令，但只有最后一个生效，CMD 会被 docker run 之后的参数替换</strong></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -p 8888:8080 tomcat ls -l  </span><br></pre></td></tr></table></figure>

<p>由于新增了 ls -l 所以会报错，因为 tomcat 最后一个 cmd 是 <code>/bin/bash</code>，ls-l 在最后一行，独占一个命令，但它本身又不是一个命令 </p>
<h5 id="ENTRYPOINT-1"><a href="#ENTRYPOINT-1" class="headerlink" title="ENTRYPOINT"></a>ENTRYPOINT</h5><blockquote>
<p><strong>docker run 之后的参数会被当做参数传递给 ENTRYPOINT，之后形成新的命令组合</strong></p>
</blockquote>
<p><img src= "/img/loading.gif" data-lazy-src="https://cdn.nlark.com/yuque/0/2020/png/326656/1590233936819-fb0eb57c-3f3e-41ef-8b2b-2f53036b6112.png" alt="image.png"></p>
<p>制作 CMD 版可以查询 IP 信息的容器，先生成 DockerFile 文件</p>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> yum install -y curl</span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> [ <span class="string">&quot;curl&quot;</span>, <span class="string">&quot;-s&quot;</span>, <span class="string">&quot;http://ip.cn&quot;</span> ]</span></span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183429.png" alt="image.png"></p>
<p>如果我们希望显示 HTTP 头信息，就需要加上 -i 参数，但是这几这样会报错的。</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183439.png" alt="image.png"></p>
<p>我们可以看到可执行文件找不到的报错，executable file not found。</p>
<p>之前我们说过，跟在镜像名后面的是 command，运行时会替换 CMD 的默认值。</p>
<p>因此这里的 -i 替换了原来的 CMD，而不是添加在原来的 <code>curl -s http://ip.cn</code> 后面。而 -i 根本不是命令，所以自然找不到。</p>
<p>那么如果我们希望加入 -i 这参数，我们就必须重新完整的输入这个命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ docker run myip curl -s http:&#x2F;&#x2F;ip.cn -i</span><br></pre></td></tr></table></figure>



<h5 id="制作-ENTROYPOINT-版查询-IP-信息的容器"><a href="#制作-ENTROYPOINT-版查询-IP-信息的容器" class="headerlink" title="制作 ENTROYPOINT 版查询 IP 信息的容器"></a>制作 ENTROYPOINT 版查询 IP 信息的容器</h5><blockquote>
<p>注意：此处使用的是 ENTRYPOINT</p>
</blockquote>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> yum install -y curl</span></span><br><span class="line"><span class="keyword">ENTRYPOINT</span><span class="bash"> [ <span class="string">&quot;curl&quot;</span>, <span class="string">&quot;-s&quot;</span>, <span class="string">&quot;http://ip.cn&quot;</span> ]</span></span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183446.png" alt="image.png"></p>
<h4 id="小总结-1"><a href="#小总结-1" class="headerlink" title="小总结"></a>小总结</h4><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183449.png" alt="image.png"></p>
<h2 id="Docker-常用安装"><a href="#Docker-常用安装" class="headerlink" title="Docker 常用安装"></a>Docker 常用安装</h2><h3 id="总体步骤"><a href="#总体步骤" class="headerlink" title="总体步骤"></a>总体步骤</h3><ol>
<li> 搜索镜像</li>
<li> 拉取镜像</li>
<li> 查看镜像</li>
<li> 启动镜像</li>
<li> 停止容器</li>
<li> 移除容器</li>
</ol>
<h3 id="安装-mysql"><a href="#安装-mysql" class="headerlink" title="安装 mysql"></a>安装 mysql</h3><p>在 docker hub 上面查找 mysql 镜像，并且星数大于 50</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search -f stars&#x3D;50 mysql</span><br></pre></td></tr></table></figure>

<h5 id="从-docker-hub-上-国内镜像源加速器-拉取-mysql-镜像到本地标签为-5-6"><a href="#从-docker-hub-上-国内镜像源加速器-拉取-mysql-镜像到本地标签为-5-6" class="headerlink" title="从 docker hub 上 (国内镜像源加速器) 拉取 mysql 镜像到本地标签为 5.6"></a>从 docker hub 上 (国内镜像源加速器) 拉取 mysql 镜像到本地标签为 5.6</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull mysql:5.6</span><br></pre></td></tr></table></figure>

<h5 id="使用-mysql5-6-镜像创建容器-也叫运行镜像"><a href="#使用-mysql5-6-镜像创建容器-也叫运行镜像" class="headerlink" title="使用 mysql5.6 镜像创建容器 (也叫运行镜像)"></a>使用 mysql5.6 镜像创建容器 (也叫运行镜像)</h5><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">docker run -p <span class="number">12345</span>:<span class="number">3306</span> --name mysql -v /zzyyuse/mysql/conf:/etc/mysql/conf.d -v /zzyyuse/mysql/logs:/logs \</span><br><span class="line">-v /zzyyuse/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<span class="number">123456</span> -d mysql:<span class="number">5</span>.<span class="number">6</span></span><br><span class="line"></span><br><span class="line">命令说明：</span><br><span class="line">-p <span class="number">12345</span>:<span class="number">3306</span>：将主机的 <span class="number">12345</span> 端口映射到 docker 容器的 <span class="number">3306</span> 端口。</span><br><span class="line"></span><br><span class="line">--name mysql：运行服务名字</span><br><span class="line"></span><br><span class="line">-v /zzyyuse/mysql/conf:/etc/mysql/conf.d ：将主机 /zzyyuse/mysql 目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/conf.d</span><br><span class="line"></span><br><span class="line">-v /zzyyuse/mysql/logs:/logs：将主机 /zzyyuse/mysql 目录下的 logs 目录挂载到容器的 /logs。</span><br><span class="line"></span><br><span class="line">-v /zzyyuse/mysql/data:/var/lib/mysql ：将主机 /zzyyuse/mysql 目录下的 data 目录挂载到容器的 /var/lib/mysql </span><br><span class="line"></span><br><span class="line">-e MYSQL_ROOT_PASSWORD=<span class="number">123456</span>：初始化 root 用户的密码。</span><br><span class="line"></span><br><span class="line">-d mysql:<span class="number">5</span>.<span class="number">6</span> : 后台程序运行 mysql5.<span class="number">6</span></span><br><span class="line"></span><br><span class="line">docker exec -it MySQL 运行成功后的容器 ID /bin/bash</span><br></pre></td></tr></table></figure>

<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183453.png" alt="image.png"></p>
<h5 id="外部-Win10-也来连接运行在-dokcer-上的-mysql-服务"><a href="#外部-Win10-也来连接运行在-dokcer-上的-mysql-服务" class="headerlink" title="外部 Win10 也来连接运行在 dokcer 上的 mysql 服务"></a>外部 Win10 也来连接运行在 dokcer 上的 mysql 服务</h5><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183456.png" alt="image.png"></p>
<h5 id="数据备份小测试"><a href="#数据备份小测试" class="headerlink" title="数据备份小测试"></a>数据备份小测试</h5><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker exec myql服务容器ID sh -c <span class="string">&#x27;exec mysqldump --all-databases -uroot -p&quot;123456&quot; &#x27;</span> &gt; /zzyyuse/all-databases.sql</span><br></pre></td></tr></table></figure>

<h2 id="本地镜像发布到阿里云"><a href="#本地镜像发布到阿里云" class="headerlink" title="本地镜像发布到阿里云"></a>本地镜像发布到阿里云</h2><p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183500.png" alt="image.png"></p>
<h3 id="镜像的生成方法"><a href="#镜像的生成方法" class="headerlink" title="镜像的生成方法"></a>镜像的生成方法</h3><ol>
<li>创建 DockerFile</li>
<li> 从容器创建一个新的镜像</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit [OPTIONS] 容器ID [REPOSITORY[:TAG]]</span><br></pre></td></tr></table></figure>

<p>OPTIONS 说明：</p>
<p>-a : 提交的镜像作者；</p>
<p>-m : 提交时的说明文字；</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183507.png" alt="image.png"></p>
<h3 id="推送到阿里云"><a href="#推送到阿里云" class="headerlink" title="推送到阿里云"></a>推送到阿里云</h3><ol>
<li>本地镜像素材原型</li>
</ol>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183511.png" alt="image.png"></p>
<ol>
<li>阿里云开发者平台的**<a target="_blank" rel="noopener" href="https://cr.console.aliyun.com/cn-hangzhou/instances/repositories">容器镜像服务</a> 网站，设置命名空间与仓库名称**</li>
</ol>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183514.png" alt="image.png"></p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183517.png" alt="image.png">**</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183521.png" alt="image.png"></p>
<p><strong>开始推送</strong></p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183533.png" alt="image.png"></p>
<p><strong>可以看到推送成功</strong></p>
<p><img src= "/img/loading.gif" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210404183536.png" alt="image.png"></p>
<h3 id="阿里云上的镜像下载到本地"><a href="#阿里云上的镜像下载到本地" class="headerlink" title="阿里云上的镜像下载到本地"></a>阿里云上的镜像下载到本地</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo docker pull registry.cn-hangzhou.aliyuncs.com&#x2F;nbutluo&#x2F;mycentos:[镜像版本号]</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Steve</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://steve6636.gitee.io/2022/03/06/Docker%20%E7%AC%94%E8%AE%B0_20210404/">https://steve6636.gitee.io/2022/03/06/Docker%20%E7%AC%94%E8%AE%B0_20210404/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://steve6636.gitee.io" target="_blank">小波的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Docker/">Docker</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/forkiller/picture/raw/master/img/20220306223850.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" data-lazy-src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" data-lazy-src="/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/03/06/Junit&amp;jdk8%E6%96%B0%E7%89%B9%E6%80%A7/"><img class="prev-cover" data-lazy-src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.05sun.com%2Fup%2F2006%2F2020611101156542640.png&amp;refer=http%3A%2F%2Fimg.05sun.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1649170568&amp;t=1d3529a6cb00dac37673cce4473d9a89" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">jdk8 新特性.md</div></div></a></div><div class="next-post pull-right"><a href="/2021/10/24/MySQL_%E5%91%A8%E9%98%B3%202018%20%E8%A7%86%E9%A2%91%E7%AC%94%E8%AE%B0_20210405/"><img class="next-cover" data-lazy-src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info"></div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20210327175316.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Steve</div><div class="author-info__description">路漫漫其修远兮，吾将上下而求索</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/forkiller"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/forkiller" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1518109600@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">冷灯看剑，剑上几番功名？炉香无须计苍生，纵一川烟逝，万丈云埋，孤阳还照古陵</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Docker-%E7%AC%94%E8%AE%B0"><span class="toc-text">Docker 笔记</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E8%BF%90%E8%A1%8C%E5%8E%9F%E7%90%86"><span class="toc-text">Docker 运行原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E7%9A%84%E7%BB%84%E6%88%90"><span class="toc-text">Docker 的组成</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%EF%BC%88image%EF%BC%89"><span class="toc-text">镜像（image）</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E4%B8%8E%E5%AE%B9%E5%99%A8%E7%9A%84%E5%85%B3%E7%B3%BB"><span class="toc-text">镜像与容器的关系</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%EF%BC%88container%EF%BC%89"><span class="toc-text">容器（container）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%93%E5%BA%93-Registry"><span class="toc-text">仓库 (Registry)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E5%AE%89%E8%A3%85"><span class="toc-text">Docker 安装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-text">准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85-docker"><span class="toc-text">安装 docker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8-docker"><span class="toc-text">启动 docker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95-run"><span class="toc-text">测试 (run)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F"><span class="toc-text">配置镜像加速</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E7%B3%BB%E7%BB%9F%E5%91%BD%E4%BB%A4"><span class="toc-text">其他系统命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%B8%E8%BD%BD-docker"><span class="toc-text">卸载 docker</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-text">Docker 常用命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%AE%E5%8A%A9%E5%91%BD%E4%BB%A4"><span class="toc-text">帮助命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E5%91%BD%E4%BB%A4"><span class="toc-text">镜像命令</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%95%9C%E5%83%8F"><span class="toc-text">查看镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2%E9%95%9C%E5%83%8F"><span class="toc-text">搜索镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8B%89%E5%8F%96%E9%95%9C%E5%83%8F"><span class="toc-text">拉取镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E9%95%9C%E5%83%8F"><span class="toc-text">删除镜像</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%91%BD%E4%BB%A4"><span class="toc-text">容器命令</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8"><span class="toc-text">查看容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B0%E5%BB%BA%E5%B9%B6%E5%90%AF%E5%8A%A8%E5%AE%B9%E5%99%A8"><span class="toc-text">新建并启动容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%90%AF%E5%8A%A8%E5%81%9C%E6%AD%A2%E9%87%8D%E5%90%AF"><span class="toc-text">容器启动停止重启</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%80%E5%87%BA%E5%AE%B9%E5%99%A8"><span class="toc-text">退出容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%AE%B9%E5%99%A8"><span class="toc-text">删除容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E4%BA%A4%E4%BA%92%E5%BC%8F%E5%AE%B9%E5%99%A8"><span class="toc-text">启动交互式容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E5%AE%88%E6%8A%A4%E5%BC%8F%E5%AE%B9%E5%99%A8"><span class="toc-text">启动守护式容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E6%97%A5%E5%BF%97"><span class="toc-text">查看容器日志</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E5%86%85%E8%BF%90%E8%A1%8C%E7%9A%84%E8%BF%9B%E7%A8%8B"><span class="toc-text">查看容器内运行的进程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E5%86%85%E9%83%A8%E7%BB%86%E8%8A%82"><span class="toc-text">查看容器内部细节</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E9%97%B4%E7%9A%84%E4%BA%A4%E4%BA%92%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%89"><span class="toc-text">容器间的交互（重点）</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%9B%B4%E6%8E%A5%E8%BF%9B%E5%85%A5%E5%AE%B9%E5%99%A8"><span class="toc-text">直接进入容器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%87%8D%E6%96%B0%E8%BF%9B%E5%85%A5%E9%80%80%E5%87%BA%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-text">重新进入退出的容器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%B8%8A%E8%BF%B0%E4%B8%A4%E4%B8%AA%E5%8C%BA%E5%88%AB"><span class="toc-text">上述两个区别</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BB%8E%E5%AE%B9%E5%99%A8%E5%86%85%E6%8B%B7%E8%B4%9D%E6%96%87%E4%BB%B6%E5%88%B0%E4%B8%BB%E6%9C%BA%E4%B8%8A"><span class="toc-text">从容器内拷贝文件到主机上</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E9%95%9C%E5%83%8F"><span class="toc-text">Docker 镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8A%A0%E8%BD%BD%E5%8E%9F%E7%90%86"><span class="toc-text">加载原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E5%B1%82%E7%9A%84%E9%95%9C%E5%83%8F"><span class="toc-text">分层的镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E7%9A%84%E7%89%B9%E7%82%B9"><span class="toc-text">镜像的特点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-%E9%95%9C%E5%83%8F-commit"><span class="toc-text">Docker 镜像 commit</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A0%BC%E5%BC%8F"><span class="toc-text">基本格式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E9%95%9C%E5%83%8F"><span class="toc-text">生成镜像</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-text">Docker 容器数据卷</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-text">数据卷</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9B%B4%E6%8E%A5%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%B7%BB%E5%8A%A0"><span class="toc-text">直接命令行添加</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E5%8D%B7%E6%98%AF%E5%90%A6%E6%8C%82%E8%BD%BD%E6%88%90%E5%8A%9F"><span class="toc-text">查看数据卷是否挂载成功</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%92%8C%E5%AE%BF%E4%B8%BB%E6%9C%BA%E4%B9%8B%E9%97%B4%E6%95%B0%E6%8D%AE%E5%85%B1%E4%BA%AB"><span class="toc-text">容器和宿主机之间数据共享</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%81%9C%E6%AD%A2%E9%80%80%E5%87%BA%E5%90%8E%EF%BC%8C%E4%B8%BB%E6%9C%BA%E4%BF%AE%E6%94%B9%E5%90%8E%E6%95%B0%E6%8D%AE%E6%98%AF%E5%90%A6%E5%90%8C%E6%AD%A5"><span class="toc-text">容器停止退出后，主机修改后数据是否同步</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%A6%E6%9D%83%E9%99%90%E5%91%BD%E4%BB%A4%E6%B7%BB%E5%8A%A0"><span class="toc-text">带权限命令添加</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DockFile-%E6%96%B9%E5%BC%8F%E6%B7%BB%E5%8A%A0"><span class="toc-text">DockFile 方式添加</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E6%A0%B9%E7%9B%AE%E5%BD%95%E4%B8%8B%E6%96%B0%E5%BB%BA-mydocker-%E6%96%87%E4%BB%B6%E5%A4%B9%E5%B9%B6%E8%BF%9B%E5%85%A5%EF%BC%8C%E7%BC%96%E8%BE%91%E4%B8%80%E4%B8%AA-DockerFile"><span class="toc-text">1. 根目录下新建 mydocker 文件夹并进入，编辑一个 DockerFile</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-File-%E6%9E%84%E5%BB%BA"><span class="toc-text">2. File 构建</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E8%8E%B7%E5%BE%97%E4%B8%80%E4%B8%AA%E6%96%B0%E9%95%9C%E5%83%8F-zzyy-centos"><span class="toc-text">3. 获得一个新镜像 zzyy&#x2F;centos</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-run-%E8%BF%90%E8%A1%8C%E4%B8%8A%E4%B8%80%E6%AD%A5%E7%94%9F%E6%88%90%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-text">4. run 运行上一步生成的容器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-%E5%AF%B9%E5%BA%94%E7%9A%84%E4%B8%BB%E6%9C%BA%E7%9B%AE%E5%BD%95%E5%9C%B0%E5%9D%80%E5%93%AA%EF%BC%9F%EF%BC%9F"><span class="toc-text">5. 对应的主机目录地址哪？？</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%8D%B7%E5%AE%B9%E5%99%A8"><span class="toc-text">数据卷容器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%BB%E4%BD%93%E4%BB%8B%E7%BB%8D"><span class="toc-text">总体介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E9%97%B4%E4%BC%A0%E9%80%92%E5%85%B1%E4%BA%AB"><span class="toc-text">容器间传递共享</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E5%85%88%E5%90%AF%E5%8A%A8%E4%B8%80%E4%B8%AA%E7%88%B6%E5%AE%B9%E5%99%A8-dc01"><span class="toc-text">1. 先启动一个父容器 dc01</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-dc02-dc03-%E7%BB%A7%E6%89%BF%E8%87%AA-dc01"><span class="toc-text">2. dc02&#x2F;dc03 继承自 dc01</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E5%9B%9E%E5%88%B0-dc01-%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0-02-03-%E5%90%84%E8%87%AA%E6%B7%BB%E5%8A%A0%E7%9A%84%E9%83%BD%E8%83%BD%E5%85%B1%E4%BA%AB%E4%BA%86"><span class="toc-text">3. 回到 dc01 可以看到 02&#x2F;03 各自添加的都能共享了</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%A0%E9%99%A4-dc01%EF%BC%8Cdc02-%E4%BF%AE%E6%94%B9%E5%90%8E-dc03-%E5%8F%AF%E5%90%A6%E8%AE%BF%E9%97%AE"><span class="toc-text">删除 dc01，dc02 修改后 dc03 可否访问</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%A0%E9%99%A4-dc02-%E5%90%8E-dc03-%E5%8F%AF%E5%90%A6%E8%AE%BF%E9%97%AE%EF%BC%8C%E5%86%8D%E8%BF%9B%E4%B8%80%E6%AD%A5%E7%BB%A7%E6%89%BF"><span class="toc-text">删除 dc02 后 dc03 可否访问，再进一步继承</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B0%E5%BB%BA-dc04-%E7%BB%A7%E6%89%BF-dc03-%E5%90%8E%E5%86%8D%E5%88%A0%E9%99%A4-dc03"><span class="toc-text">新建 dc04 继承 dc03 后再删除 dc03</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DockerFile-%E8%A7%A3%E6%9E%90"><span class="toc-text">DockerFile 解析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%B8%89%E6%AD%A5%E9%AA%A4"><span class="toc-text">构建三步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E8%BF%87%E7%A8%8B%E8%A7%A3%E6%9E%90"><span class="toc-text">构建过程解析</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-text">基础知识</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%A7%E8%87%B4%E6%B5%81%E7%A8%8B"><span class="toc-text">大致流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B0%8F%E6%80%BB%E7%BB%93"><span class="toc-text">小总结</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%9D%E7%95%99%E5%AD%97%E6%8C%87%E4%BB%A4"><span class="toc-text">保留字指令</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#FROM"><span class="toc-text">FROM</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#RUN"><span class="toc-text">RUN</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#EXPOSE"><span class="toc-text">EXPOSE</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#WORKDIR"><span class="toc-text">WORKDIR</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ENV"><span class="toc-text">ENV</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ADD"><span class="toc-text">ADD</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#COPY"><span class="toc-text">COPY</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#VOLUME"><span class="toc-text">VOLUME</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#CMD"><span class="toc-text">CMD</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ENTRYPOINT"><span class="toc-text">ENTRYPOINT</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ONBUILD"><span class="toc-text">ONBUILD</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B"><span class="toc-text">案例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Base-%E9%95%9C%E5%83%8F-scratch"><span class="toc-text">Base 镜像 (scratch)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89-centos-%E9%95%9C%E5%83%8F"><span class="toc-text">自定义 centos 镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E5%87%86%E5%A4%87-DockerFile"><span class="toc-text">1. 准备 DockerFile</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E6%9E%84%E5%BB%BA%E8%BF%90%E8%A1%8C"><span class="toc-text">2. 构建运行</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E8%BF%90%E8%A1%8C"><span class="toc-text">3. 运行</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-%E5%88%97%E5%87%BA%E9%95%9C%E5%83%8F%E7%9A%84%E5%8F%98%E6%9B%B4%E5%8E%86%E5%8F%B2"><span class="toc-text">4. 列出镜像的变更历史</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CMD-ENTRYPOINT-%E6%8C%87%E4%BB%A4%E5%AF%B9%E6%AF%94"><span class="toc-text">CMD&#x2F;ENTRYPOINT 指令对比</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#CMD-1"><span class="toc-text">CMD</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ENTRYPOINT-1"><span class="toc-text">ENTRYPOINT</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%B6%E4%BD%9C-ENTROYPOINT-%E7%89%88%E6%9F%A5%E8%AF%A2-IP-%E4%BF%A1%E6%81%AF%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-text">制作 ENTROYPOINT 版查询 IP 信息的容器</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B0%8F%E6%80%BB%E7%BB%93-1"><span class="toc-text">小总结</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-%E5%B8%B8%E7%94%A8%E5%AE%89%E8%A3%85"><span class="toc-text">Docker 常用安装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E4%BD%93%E6%AD%A5%E9%AA%A4"><span class="toc-text">总体步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85-mysql"><span class="toc-text">安装 mysql</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BB%8E-docker-hub-%E4%B8%8A-%E5%9B%BD%E5%86%85%E9%95%9C%E5%83%8F%E6%BA%90%E5%8A%A0%E9%80%9F%E5%99%A8-%E6%8B%89%E5%8F%96-mysql-%E9%95%9C%E5%83%8F%E5%88%B0%E6%9C%AC%E5%9C%B0%E6%A0%87%E7%AD%BE%E4%B8%BA-5-6"><span class="toc-text">从 docker hub 上 (国内镜像源加速器) 拉取 mysql 镜像到本地标签为 5.6</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-mysql5-6-%E9%95%9C%E5%83%8F%E5%88%9B%E5%BB%BA%E5%AE%B9%E5%99%A8-%E4%B9%9F%E5%8F%AB%E8%BF%90%E8%A1%8C%E9%95%9C%E5%83%8F"><span class="toc-text">使用 mysql5.6 镜像创建容器 (也叫运行镜像)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%96%E9%83%A8-Win10-%E4%B9%9F%E6%9D%A5%E8%BF%9E%E6%8E%A5%E8%BF%90%E8%A1%8C%E5%9C%A8-dokcer-%E4%B8%8A%E7%9A%84-mysql-%E6%9C%8D%E5%8A%A1"><span class="toc-text">外部 Win10 也来连接运行在 dokcer 上的 mysql 服务</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%A4%87%E4%BB%BD%E5%B0%8F%E6%B5%8B%E8%AF%95"><span class="toc-text">数据备份小测试</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E5%9C%B0%E9%95%9C%E5%83%8F%E5%8F%91%E5%B8%83%E5%88%B0%E9%98%BF%E9%87%8C%E4%BA%91"><span class="toc-text">本地镜像发布到阿里云</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E7%9A%84%E7%94%9F%E6%88%90%E6%96%B9%E6%B3%95"><span class="toc-text">镜像的生成方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A8%E9%80%81%E5%88%B0%E9%98%BF%E9%87%8C%E4%BA%91"><span class="toc-text">推送到阿里云</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%98%BF%E9%87%8C%E4%BA%91%E4%B8%8A%E7%9A%84%E9%95%9C%E5%83%8F%E4%B8%8B%E8%BD%BD%E5%88%B0%E6%9C%AC%E5%9C%B0"><span class="toc-text">阿里云上的镜像下载到本地</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/03/06/Mysql%E7%9A%84%E4%BC%98%E5%8C%96-%E8%87%AA%E6%88%91%E6%8F%8F%E8%BF%B0/" title="MySQL 的优化.md"><img data-lazy-src="https://img2.baidu.com/it/u=1814268193,3619863984&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=632&amp;h=500" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="MySQL 的优化.md"/></a><div class="content"><a class="title" href="/2022/03/06/Mysql%E7%9A%84%E4%BC%98%E5%8C%96-%E8%87%AA%E6%88%91%E6%8F%8F%E8%BF%B0/" title="MySQL 的优化.md">MySQL 的优化.md</a><time datetime="2022-03-06T14:54:18.000Z" title="发表于 2022-03-06 22:54:18">2022-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/06/MySQL%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%91%BD%E4%BB%A4-20210722/" title="MySQL 数据库备份命令.md"><img data-lazy-src="https://img2.baidu.com/it/u=1814268193,3619863984&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=632&amp;h=500" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="MySQL 数据库备份命令.md"/></a><div class="content"><a class="title" href="/2022/03/06/MySQL%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%91%BD%E4%BB%A4-20210722/" title="MySQL 数据库备份命令.md">MySQL 数据库备份命令.md</a><time datetime="2022-03-06T14:47:18.000Z" title="发表于 2022-03-06 22:47:18">2022-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/06/Git-study/" title="git 笔记.md"><img data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20220306225241.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="git 笔记.md"/></a><div class="content"><a class="title" href="/2022/03/06/Git-study/" title="git 笔记.md">git 笔记.md</a><time datetime="2022-03-06T14:47:18.000Z" title="发表于 2022-03-06 22:47:18">2022-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/06/Junit&amp;jdk8%E6%96%B0%E7%89%B9%E6%80%A7/" title="jdk8 新特性.md"><img data-lazy-src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.05sun.com%2Fup%2F2006%2F2020611101156542640.png&amp;refer=http%3A%2F%2Fimg.05sun.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1649170568&amp;t=1d3529a6cb00dac37673cce4473d9a89" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="jdk8 新特性.md"/></a><div class="content"><a class="title" href="/2022/03/06/Junit&amp;jdk8%E6%96%B0%E7%89%B9%E6%80%A7/" title="jdk8 新特性.md">jdk8 新特性.md</a><time datetime="2022-03-06T14:47:18.000Z" title="发表于 2022-03-06 22:47:18">2022-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/06/Docker%20%E7%AC%94%E8%AE%B0_20210404/" title="Docker 笔记.md"><img data-lazy-src="https://gitee.com/forkiller/picture/raw/master/img/20220306223850.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker 笔记.md"/></a><div class="content"><a class="title" href="/2022/03/06/Docker%20%E7%AC%94%E8%AE%B0_20210404/" title="Docker 笔记.md">Docker 笔记.md</a><time datetime="2022-03-06T14:29:58.000Z" title="发表于 2022-03-06 22:29:58">2022-03-06</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gitee.com/forkiller/picture/raw/master/img/20220306223850.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Steve</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  if (typeof gtag === 'function') {
    gtag('config', '', {'page_path': window.location.pathname});
  }

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})


document.addEventListener('pjax:send', function () {
  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>